package booroo.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JRadioButton;

import booroo.dao.MemberDAO;
import booroo.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class addMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JTextField uid;
	private JPasswordField pwd;
	private JPasswordField repwd;
	private MemberDAO mdao = new MemberDAO();
	// static을 사용하면 new연산자 없이 객체를 바로 사용할 수 있음
	// 하지만, static없이 클래스수준의 멤버변수로 선언해서 
	// 모든 메서드가 다 같이 쓸수 있도록 하는 것도 좋음

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			addMember dialog = new addMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public addMember() {
		setBounds(100, 100, 640, 480);
		setTitle("회원가입");
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("굴림", Font.BOLD, 14));
		
		name = new JTextField();
		name.setColumns(10);
		
		uid = new JTextField();
		uid.setPreferredSize(new Dimension(7, 21));
		uid.setColumns(10);
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("굴림", Font.BOLD, 14));
		
		pwd = new JPasswordField();
		
		repwd = new JPasswordField();
		
		JButton checkUidBtn = new JButton("중복");
		
		JRadioButton isMale = new JRadioButton("남자");
		isMale.setFont(new Font("굴림", Font.BOLD, 14));
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		isFemale.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("굴림", Font.BOLD, 14));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtName))
							.addGap(31)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
								.addComponent(name, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(uid, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(checkUidBtn))
								.addComponent(pwd)))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
								.addComponent(txtRepwd, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(isMale)
									.addPreferredGap(ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
									.addComponent(isFemale))
								.addComponent(repwd, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))))
					.addGap(300))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(checkUidBtn)))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(9)
							.addComponent(txtUid)))
					.addGap(9)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtPwd)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(9)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtRepwd)
						.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(isMale)
						.addComponent(isFemale)
						.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(247, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton joinBtn = new JButton("가입확인");
				joinBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(!Arrays.equals(pwd.getPassword(), repwd.getPassword()))
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않습니다!");
						else {
							
							MemberVO m = new MemberVO(null,name.getText(),uid.getText(),new String(pwd.getPassword()),"" , null);
							if(mdao.insertMember(m) > 0)
								JOptionPane.showMessageDialog(null, "회원가입완료!");
						}
					}
				});
				joinBtn.setActionCommand("OK");
				buttonPane.add(joinBtn);
				getRootPane().setDefaultButton(joinBtn);
			}
		}
	}
}
